package com.ruoyi.web.controller.devices;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.system.domain.NurturanceConfig;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.DeviceInfo;
import com.ruoyi.system.service.IDeviceInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 设备信息Controller
 *
 * @author ruoyi
 * @date 2025-04-15
 */
@RestController
@RequestMapping("/system/device")
public class DeviceInfoController extends BaseController {

    @Log(title = "设备信息", businessType = BusinessType.INSERT)
    @PostMapping
    @Anonymous
    public AjaxResult add(@RequestBody DeviceInfo deviceInfo) {
        return toAjax(deviceInfoService.insertDeviceInfo(deviceInfo));
    }

    @Autowired
    private IDeviceInfoService deviceInfoService;

    @Autowired
    private RedisCache redisCache;

    private static final String DEVICE_HASH_KEY = "device:info";
    private static final String UPDATE_CHANNEL = "device:updates";
    private static final String UPDATE_NURTURANCE = "device:nurturance";


    /**
     * 查询设备信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:device:list')")
    @GetMapping("/list")
    public TableDataInfo list(DeviceInfo deviceInfo) {
        startPage();
        List<DeviceInfo> list = deviceInfoService.selectDeviceInfoList(deviceInfo);
        return getDataTable(list);
    }

    /**
     * 导出设备信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:device:export')")
    @Log(title = "设备信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DeviceInfo deviceInfo) {
        List<DeviceInfo> list = deviceInfoService.selectDeviceInfoList(deviceInfo);
        ExcelUtil<DeviceInfo> util = new ExcelUtil<DeviceInfo>(DeviceInfo.class);
        util.exportExcel(response, list, "设备信息数据");
    }

    /**
     * 获取设备信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:device:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(deviceInfoService.selectDeviceInfoById(id));
    }

    /**
     * 新增设备信息
     */
//    @PreAuthorize("@ss.hasPermi('system:device:add')")


    /**
     * 修改设备信息
     */
    @PreAuthorize("@ss.hasPermi('system:device:edit')")
    @Log(title = "设备信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DeviceInfo deviceInfo) {
        return toAjax(deviceInfoService.updateDeviceInfo(deviceInfo));
    }

    /**
     * 删除设备信息
     */
    @PreAuthorize("@ss.hasPermi('system:device:remove')")
    @Log(title = "设备信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(deviceInfoService.deleteDeviceInfoByIds(ids));
    }

    /**
     * 修改设备信息
     */
    @PreAuthorize("@ss.hasPermi('system:device:edit')")
    @Log(title = "更新设备", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public AjaxResult updateDevice() {
        redisCache.publish(UPDATE_CHANNEL,"1");
        return toAjax(1);
    }

    /**
     * 修改设备信息
     */
    @PreAuthorize("@ss.hasPermi('system:device:edit')")
    @Log(title = "更新设备", businessType = BusinessType.UPDATE)
    @GetMapping("/groupList")
    public AjaxResult getGroupList() {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("data", deviceInfoService.getGroupList());
        return ajax;
    }


    /**
     * 开始打粉
     */
    @PreAuthorize("@ss.hasPermi('system:device:edit')")
    @Log(title = "开始打粉", businessType = BusinessType.UPDATE)
    @PostMapping("/nurturance")
    public AjaxResult startNurturance(@RequestBody NurturanceConfig nurturanceConfig) {

        redisCache.publish(UPDATE_NURTURANCE,nurturanceConfig.getGname());
        return toAjax(1);
    }


    /**
     * 获取设备信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:device:query')")
    @GetMapping(value = "/power/{ids}")
    public AjaxResult power(@PathVariable Long[] ids) {
        return success(deviceInfoService.powerDevice(ids));
    }


    /**
     * 获取设备信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:device:query')")
    @GetMapping(value = "/stoppower/{ids}")
    public AjaxResult stopPower(@PathVariable Long[] ids) {
        return success(deviceInfoService.powerDevice(ids));
    }

    /**
     * 获取设备信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:device:query')")
    @GetMapping(value = "/pause/{ids}")
    public AjaxResult pause(@PathVariable Long[] ids) {
        return success(deviceInfoService.pauseDevice(ids));
    }

    /**
     * 获取设备信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:device:query')")
    @GetMapping(value = "/stoppower/{ids}")
    public AjaxResult pauseAll() {
        return success(deviceInfoService.pauseAllDevice());
    }



}
